//
//  EndSOSView.m
//  location
//
//  Created by Dongxiang Xu on 2025/8/28.
//

#import "EndSOSView.h"

@interface EndSOSView ()<UIGestureRecognizerDelegate>

@property(nonatomic,strong) UIView *infoView;
@property(nonatomic,strong) UIView *wave;
@property(nonatomic,strong) UIView *aWave;
@property(nonatomic,strong) UILabel *sos;
@property(nonatomic,strong) UIView *slider;
@property(nonatomic,strong) UIImageView *sosSlider;
@property(nonatomic,strong) UIView *sliderView;
@property(nonatomic,strong) NSDictionary *groupInfo;

@end

@implementation EndSOSView

- (instancetype)initWithFrame:(CGRect)frame
                    GroupInfo:(NSDictionary *)groupInfo
                       
{
    self = [super initWithFrame:frame];
    if (self) {
        self.groupInfo = groupInfo;
        
        UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        view.image = [UIImage imageNamed:@"sos_bg"];
        [self addSubview:view];
                        
        _infoView = [[UIView alloc] initWithFrame:CGRectMake(12, SCREEN_HEIGHT, SCREEN_WIDTH-24, 440)];
        _infoView.backgroundColor = RealWhiteColor;
        _infoView.layer.masksToBounds = YES;
        [_infoView.layer setCornerRadius:40];
        [self addSubview:_infoView];
        
        UIButton *close = [[UIButton alloc] initWithFrame:CGRectMake(_infoView.width-42, 20, 22, 22)];
        [close setBackgroundImage:[UIImage imageNamed:@"map_close"] forState:UIControlStateNormal];
        [_infoView addSubview:close];
        [[close rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [UIView animateWithDuration:0.3 animations:^{
                self.infoView.y = SCREEN_HEIGHT;
            } completion:^(BOOL finished) {
                [self removeFromSuperview];
            }];
        }];
        
        _sos = [[UILabel alloc] initWithFrame:CGRectMake((_infoView.width-96)/2, 70, 96, 96)];
        _sos.layer.masksToBounds = YES;
        [_sos.layer setCornerRadius:48];
        _sos.backgroundColor = SOSColor;
        _sos.textColor = RealWhiteColor;
        _sos.font = MEDIUM_FONT(30);
        _sos.textAlignment = NSTextAlignmentCenter;
        _sos.text = @"SOS";
        [_infoView addSubview:_sos];
        
        _wave = [[UIView alloc] initWithFrame:CGRectMake(_sos.x, _sos.y, _sos.width, _sos.height)];
        _wave.backgroundColor = SOSColor;
        _wave.layer.masksToBounds = YES;
        [_wave.layer setCornerRadius:_wave.width/2];
        [_infoView insertSubview:_wave belowSubview:_sos];
                
        _aWave = [[UIView alloc] initWithFrame:CGRectMake(_sos.x, _sos.y, _sos.width, _sos.height)];
        _aWave.backgroundColor = SOSColor;
        _aWave.layer.masksToBounds = YES;
        [_aWave.layer setCornerRadius:_aWave.width/2];
        [_infoView insertSubview:_aWave belowSubview:_sos];
        
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, _sos.maxY+52, _infoView.width, 34)];
        title.textColor = RealBlackColor;
        title.font = MEDIUM_FONT(28);
        title.textAlignment = NSTextAlignmentCenter;
        title.text = @"滑动取消呼救？";
        [_infoView addSubview:title];
        
        UILabel *subTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, title.maxY+6, _infoView.width, 34)];
        subTitle.textColor = GraryHeavyLightColor;
        subTitle.font = DEFAULT_FONT(14);
        subTitle.textAlignment = NSTextAlignmentCenter;
        subTitle.text = @"你的呼救已经发送给\n所有家庭成员、紧急联系人";
        subTitle.numberOfLines = 2;
        [subTitle sizeToFit];
        [subTitle setFrame:CGRectMake((_infoView.width-subTitle.width)/2, title.maxY+6, subTitle.width, subTitle.height)];
        [_infoView addSubview:subTitle];
        
        _slider = [[UIView alloc] initWithFrame:CGRectMake((_infoView.width-276)/2, subTitle.maxY+24, 276, 54)];
        _slider.layer.masksToBounds = YES;
        [_slider.layer setCornerRadius:27];
        _slider.backgroundColor = SOSColor;
        [_infoView addSubview:_slider];
        
        UILabel *sliderTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 176, _slider.height)];
        sliderTitle.textColor = RealWhiteColor;
        sliderTitle.textAlignment = NSTextAlignmentRight;
        sliderTitle.font = MEDIUM_FONT(16);
        sliderTitle.text = @"滑动取消呼救";
        [_slider addSubview:sliderTitle];
        
        _sosSlider = [[UIImageView alloc] initWithFrame:CGRectMake(_slider.width-_slider.height, 0, _slider.height, _slider.height)];
        _sosSlider.image = [UIImage imageNamed:@"sos_slider"];
        [_sosSlider.layer setCornerRadius:(_slider.height/2)];
        [_sosSlider.layer setBorderWidth:2];
        [_sosSlider.layer setBorderColor:SOSColor.CGColor];
        _sosSlider.layer.masksToBounds = YES;
        _sosSlider.userInteractionEnabled = YES;
        [_slider addSubview:_sosSlider];
        UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
        panGestureRecognizer.delegate = self;
        [_sosSlider addGestureRecognizer:panGestureRecognizer];
        
        _sliderView = [[UIView alloc] initWithFrame:CGRectMake(_sosSlider.centerX, 0, _slider.width-_sosSlider.centerX, _sosSlider.height)];
        _sliderView.backgroundColor = GraryLightColor;
        [_slider insertSubview:_sliderView belowSubview:_sosSlider];
        
        UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake((_infoView.width-68)/2, _slider.maxY+16, 68, 22)];
        [closeButton setTitle:@"不是现在" forState:UIControlStateNormal];
        [closeButton.titleLabel setFont:DEFAULT_FONT(14)];
        [closeButton setTitleColor:GraryLightColor forState:UIControlStateNormal];
        [_infoView addSubview:closeButton];
        [[closeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [UIView animateWithDuration:0.3 animations:^{
                self.infoView.y = SCREEN_HEIGHT;
            } completion:^(BOOL finished) {
                [self removeFromSuperview];
            }];
        }];
        
        [UIView animateWithDuration:0.3 animations:^{
            self.infoView.y = (SCREEN_HEIGHT-440)/2;
        }];
        
        [self waveAnimation];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self aWaveAnimation];
        });
       
    }
    return self;
}

- (void)waveAnimation
{
    if (self) {
        [UIView animateWithDuration:2 animations:^{
            [self.wave setFrame:CGRectMake(self.sos.x-30, self.sos.y-30, self.sos.width+60, self.sos.height+60)];
            self.wave.alpha = 0;
            [self.wave.layer setCornerRadius:self.wave.width/2];
        } completion:^(BOOL finished) {
            [self.wave setFrame:CGRectMake(self.sos.x, self.sos.y, self.sos.width, self.sos.height)];
            [self.wave.layer setCornerRadius:self.wave.width/2];
            self.wave.alpha = 1;
            [self waveAnimation];
        }];
    }
}

- (void)aWaveAnimation
{
    if (self) {
        [UIView animateWithDuration:2 animations:^{
            [self.aWave setFrame:CGRectMake(self.sos.x-30, self.sos.y-30, self.sos.width+60, self.sos.height+60)];
            self.aWave.alpha = 0;
            [self.aWave.layer setCornerRadius:self.aWave.width/2];
        } completion:^(BOOL finished) {
            [self.aWave setFrame:CGRectMake(self.sos.x, self.sos.y, self.sos.width, self.sos.height)];
            [self.aWave.layer setCornerRadius:self.aWave.width/2];
            self.aWave.alpha = 1;
            [self aWaveAnimation];
        }];
    }
}

- (void)handlePan:(UIPanGestureRecognizer *)recognizer
{
    CGPoint translation = [recognizer locationInView:_slider];
    self.sosSlider.x = translation.x;
    if (translation.x<0) {
        self.sosSlider.x = 0;
        UIImpactFeedbackGenerator *feedBackGenertor = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
        [feedBackGenertor impactOccurred];
        
        [UIView animateWithDuration:0.3 animations:^{
            self.infoView.y = SCREEN_HEIGHT;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
            
            // 先请求http，让后端发送push
            AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            manager.requestSerializer.timeoutInterval = 10;
            manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",@"image/jpeg",@"image/png",@"application/octet-stream",@"text/json",nil];
            NSMutableDictionary *param = [NSMutableDictionary dictionary];
            [param setValue:self.groupInfo[@"id"] forKey:@"groupId"];
            [manager GET:[NSString stringWithFormat:@"%@/apps/data/sosnotification",NETURL] parameters:param headers:@{@"Authorization":[XDXShareInstance shareInstance].access_token} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                                  
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

            }];
            
            if (self.EndSOSBlock) {
                self.EndSOSBlock();
            }
        }];
    }
    [_sliderView setFrame:CGRectMake(_sosSlider.centerX, 0, _slider.width-_sosSlider.centerX, _sosSlider.height)];
    
    if (recognizer.state == UIGestureRecognizerStateBegan){
       
    }else if (recognizer.state == UIGestureRecognizerStateEnded){
        [UIView animateWithDuration:0.3 animations:^{
            [self.sosSlider setFrame:CGRectMake(self.slider.width-self.slider.height, 0, self.slider.height, self.slider.height)];
            [self.sliderView setFrame:CGRectMake(self.sosSlider.centerX, 0, self.slider.width-self.sosSlider.centerX, self.sosSlider.height)];
        }];
    }
}


@end
